
const {stageList, stageindexDefault} = require("../../config"); 
const innerAudioInnerCtx = wx.createInnerAudioContext();

const app = getApp();

Page({
  /**
   * 组件的属性列表
   */
  // properties: {
  //   charDictList: Array,
  // },

  data: {
    // return {
      // renderCharList: [],
      stageCharList: [],
      randomOptionalCharList: [],
      charDictList: [],
      stageindex: stageindexDefault,
      optionalCharAmount: 2,
    // };
  },

  onLoad(options){
    const that = this;
    const stageindex = options.stageindex ? options.stageindex : that.data.stageindex;
    this.setData({
      stageindex,
    });
    app.globalData.charDoc
      .where({
        stageindex,
      })
      // .limit(this.data.optionalCharAmount)
      .get().then(res=>{
      console.log('77-77 coursePage.js:::::onShow:::success::res:', res);
            // const userInfoJson = JSON.stringify(res.data.user);
            const stageCharList = res.data.map(charDict=>{
              return {
                char: charDict.char,
                pinyin: charDict.pinyin,
                stageindex: charDict.stageindex,
                voice: charDict.voice,
              };
            });
            // const randomOptionalCharList = that.getRandomOptionalCharList(someCloudCharList);
            // const backgroundColorList = randomOptionalCharList.map((item,index)=>{
            //   index = index % 100;
            //   return `#${index}${index}${index}`;
            // });
            that.setData({
              // randomOptionalCharList,
              stageCharList,
              // backgroundColorList,
            });
    }).catch(err=>{
      console.error(err);
    });
  },

  // /**
  //  * 组件的初始数据
  //  */
  // data: {
  //   stageList,
  //   currentIndex: 0,
  //   backgroundColorList: [],
  // },
  
  getRandomOptionalCharList(someCloudCharList){
    console.log("67-67 someCloudCharList: ", someCloudCharList)
    let randomCharList = [];
    let randomIndex = 0;
    let char;
    console.log("76-76 this.data.optionalCharAmount: ", this.data.optionalCharAmount, "this.data:", this.data);
    for(let i=0; i<this.data.optionalCharAmount; i++){
      if(someCloudCharList.length > 0){
        randomIndex = Math.floor(Math.random()*someCloudCharList.length);
        char = someCloudCharList.splice(randomIndex, 1, 0);
        console.log("79-79 i: ", i, "80-80 char:", char);
        randomCharList.push(char)
      }
    }
    return randomCharList;
  },

  /**
   * 组件的方法列表
   */
  methods: {
    swiperChange(e) {
      console.log(e);
      this.setData({
        currentIndex: e.detail.current
      });
    },
    audioPlay: function (event) {
      console.log("40-40 event:", event)
      innerAudioInnerCtx.src = event.currentTarget.dataset.voice;
      // let audioCtx = wx.createAudioContext(audioid);
      console.log("43-43 voice:", innerAudioInnerCtx.src);
      innerAudioInnerCtx.play()
      // audioCtx = null;
    },
  }
});
